package com.p900.website.service.impl;

import com.p900.commons.domain.Product;
import com.p900.website.repository.ProductRepository;
import com.p900.website.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.validation.constraints.NotNull;

/**
 * Created by lenovo on 2015/5/2.
 */
@Service
public class ProductServiceImpl implements ProductService {

    private static Logger logger =  LoggerFactory.getLogger(ProductServiceImpl.class);

    @Autowired
    private ProductRepository productRepository;

    @Override
    public Product findProduct(Long id){
        return productRepository.findOne(id);
    }

    @Override
    public Page<Product> findByPlatformId(Long platformId, Pageable pageable) {
        Assert.notNull(platformId, "platformId is null");
        Assert.notNull(pageable, "pageable is null");
        return productRepository.findByPlatformId(platformId, pageable);
    }

    @Override
    public Page<Product> findHotProducts(Pageable pageable) {
        return productRepository.findAll(pageable);
    }

    @Override
    public Page<Product> searchProducts(String key, Pageable pageable) {
        Assert.notNull(key, "search key is null");
        String name = key;
        return productRepository.findByNameLike(name, pageable);
    }

    @Override
    public Product save(Product product) {
        Assert.notNull(product, "product is null");
        return productRepository.save(product);
    }

    @Override
    public void delete(@NotNull(message = "id is null") Long id) {
        productRepository.delete(id);
    }
}
